<?

	class Fmgr0v0r01_thumb extends Fmgr0v0r01_dir{
		
		protected $valid_child_classnames = 'image';
		protected $web_types = 'jpeg,jpg,gif,png';
		
		var $original;
		var $checksum;
		
		function get_functions(){
			static $a;
			if(!$a) $a = array_merge(parent::get_functions(), array(
				'x',
				'y',
				'width',
				'height'
			));
			return $a;	
		}
		
		function get($k){
			if(!is_null($v = parent::get($k))) return $v;
			if(is_numeric(substr($k, 0, 1))) return $this->produce($k);
		}
		
		function get_primary_var($k){
			if($p = $this->primary()) return $p->get($k);	
		}
		
		function tag(){
			return $this->get_primary_var('tag');
		}
		
		function uri(){
			return $this->get_primary_var('uri');		
		}
		
		function x(){
			return $this->get_primary_var('x');
		}
		
		function y(){
			return $this->get_primary_var('y');
		}
		
		function width(){
			return $this->get_primary_var('x');
		}
		
		function height(){
			return $this->get_primary_var('y');
		}
		
		function primary(){
			if(!$d = getenv('SMUOI_THUMB_ARGS')) $d = 128;
			return $this->produce($d);	
		}
		
		function produce($n=128){
			// First just use is_file, it's quick and painless
			if($t = $this->find_thumb($n)) return $t;
			if(!$d = $this->read_user_dims($n)) return false;
			$tname = $this->make_subname($d);
			// Another check in filesystem
			if($t = $this->find_thumb($tname)) return $t;
			if(!$thumb = $this->copy_original()) return false;
			$thumb->basename = $this->thumbname($tname);
			if(!$thumb->is_webimage()) $thumb->mime_subtype = 'png';
			if(is_numeric($d)) $thumb->max = $d;
			else foreach($d as $k=>$v) $thumb->$k = $v;
			$thumb->commit();
			$this->cleanup_on_shutdown();
			return $thumb;
		}
		
		private function find_thumb($n){
			$n = $this->thumbname($n);
			if(!is_file($this->realpath($n))) return false;
			return $this->child($n);
		}
		
		private function read_user_dims($n){
			if(!preg_match_all('/(\d+)([xy])?/', $n, $m)) return false;
			// fuck it ... if you got it wrong, you got it wrong
			if((!$c = count($m[1])) || ($c > 2)) return false;
			// just an integer? return that
			if(($c == 1) && (!$m[2][0])) return $m[1][0];
			$g = array('x','y');
			foreach($m[1] as $i=>$v){
				$k = (in_array($m[2][$i], $g)) ? $m[2][$i] : $g[$i];
				if($c == 1) $k = "max_$k";
				$d[$k] = $v;
			}
			return $d;
		}
		
		private function make_subname($d){
			if(!is_array($d)) return $d;
			$n = '';
			foreach($d as $k=>$v) $n .= $v.$k;
			return $n;
		}
		
		private function thumbname($n){
			$xtn = $this->original()->is_webimage() ? $this->original()->xtn() : 'png';
			return $this->original()->checksum.'-'.$n.'.'.$xtn;	
		}
		
		private function original(){
			if(!is_null($this->original)) return $this->original;
			if(($o = $this->db()->get($this->basename())) && ($o->exists())){
				return $this->original = $o;
			}
			$this->destroy();
			return false;
		}
		
		private function copy_original(){
			if((!$file = $this->original()) || (!$new = $file->copy_to($this, false))){
				$this->destroy();
				return false;
			}
			return $new;
		}
		
		function cleanup_on_shutdown(){
			$this->checksum = $this->original()->checksum;
			smuoi_register_shutdown_function(array($this, 'cleanup'));
		}
		
		function cleanup(){
			if(!$this->checksum) return;
			$t = time();
			foreach($this->contents() as $f){
				if(substr($f, 0, strlen($this->checksum)) == $this->checksum) continue;
				$fp = $this->realpath($f);
				if((filemtime($fp)+604800) > $t) continue;
				$c = $this->child($f);
				unlink($fp);
				if($c) $c->destroy();
			}
		}
		
		function purge(){
			foreach($this->contents() as $f) unlink($this->realpath($f));
			foreach($this->cdrn(1) as $child) $child->destroy();
		}
		
		function is_destroyable(){
			return true;
		}
		
		function destroy(){
			$this->purge();
			return parent::destroy();
		}
	}

?>